package com.onccc.social.step

import com.blankj.utilcode.util.AppUtils
import com.blankj.utilcode.util.ScreenUtils
import com.onccc.social.Assists
import com.onccc.social.Assists.click
import com.onccc.social.Assists.getBoundsInScreen
import com.onccc.social.Assists.log
import com.onccc.social.Assists.paste
import com.onccc.social.AssistsWindowManager
import com.onccc.social.MainActivity
import com.onccc.social.MainActivity.Companion.getComment
import com.onccc.social.MainActivity.Companion.getFiveTime
import com.onccc.social.MainActivity.Companion.getInputTime
import com.onccc.social.MainActivity.Companion.getInterest
import com.onccc.social.MainActivity.Companion.getOneTime
import com.onccc.social.MainActivity.Companion.getTwoTime
import com.onccc.social.OverManager
import com.onccc.social.model.CommentModel
import com.onccc.social.model.ImageInterestModel
import com.onccc.social.stepper.Step
import com.onccc.social.stepper.StepCollector
import com.onccc.social.stepper.StepImpl
import com.onccc.social.stepper.StepManager
import kotlinx.coroutines.delay
import kotlin.random.Random


/**
 * @author GongChengHao
 * @date 2024/12/4
 * Description :
 */
class InstagramSocial : StepImpl() {

    var mPackageName = "com.instagram.android"

    private var maxSelect = 2
    private var selectIndex = 0
    private var mCommentModel = CommentModel()
    private var mImageInterestModel = ImageInterestModel()


    override fun onImpl(collector: StepCollector) {
        collector.next(StepTag.STEP_1) {
            OverManager.log("启动Instagram")

            AppUtils.launchApp(mPackageName)
            runIO { delay(getOneTime()) }
            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_2) {
            runIO { delay(getTwoTime()) }
            Assists.findById("com.instagram.android:id/search_tab").forEach() {
                OverManager.log("点击搜索")
                Assists.gestureViewClick(it)
                runIO { delay(getTwoTime()) }
                return@next Step.get(StepTag.STEP_3)
            }
            return@next Step.get(StepTag.STEP_2)
        }.next(StepTag.STEP_3) {
            OverManager.log("输入评论内容")
            Assists.findById("com.instagram.android:id/action_bar_search_edit_text").forEach {
                Assists.gestureViewClick(it)
                runIO { delay(getOneTime()) }
                Assists.findById("com.instagram.android:id/action_bar_search_edit_text").forEach { it1 ->
                    runIO { delay(getOneTime()) }
                    for (s in MainActivity.getKeyword()){
                        it.paste(s.toString())
                        runIO { delay(getInputTime()) }
                    }
                }
                runIO { delay(getOneTime()) }
                Assists.gestureKeyboardClick()
                runIO { delay(getTwoTime()) }
            }

            return@next Step.get(StepTag.STEP_4)
        }.next(StepTag.STEP_4) {
            runIO { delay(getTwoTime()) }
            Assists.findById("com.instagram.android:id/scrollable_tab_layout").forEach {
                val screen = it.getBoundsInScreen()
                AssistsWindowManager.switchNotTouchableAll()
                val xR = ScreenUtils.getAppScreenWidth() / 2 + java.util.Random().nextInt(200).toFloat()
                val xL = ScreenUtils.getAppScreenWidth() / 2 - java.util.Random().nextInt(200).toFloat()
                val dy = screen.bottom - screen.top
                val startY = screen.top + java.util.Random().nextInt(dy).toFloat()
                val endY = screen.top + java.util.Random().nextInt(dy).toFloat()
                OverManager.log("滑动：$xR$startY,$xL/$endY")
                Assists.gesture(
                        floatArrayOf(xR, startY), floatArrayOf(xL, endY), 0, 300L + java.util.Random().nextInt(300)
                )
                AssistsWindowManager.switchTouchableAll()
            }


            return@next Step.get(StepTag.STEP_21)
        }.next(StepTag.STEP_21) {
            runIO { delay(getTwoTime()) }
            Assists.findByText("Reels").forEach() {
                val screen = it.getBoundsInScreen()
                if (screen.bottom < 500 && screen.top < 500) {
                    OverManager.log("查看短视频列表")
                    it.click()
                }
            }
            return@next Step.get(StepTag.STEP_6)
        }.next(StepTag.STEP_5) {

            selectIndex++
            if (selectIndex > maxSelect) {
                return@next Step.get(StepTag.STEP_100)
            }

            runIO { delay(getFiveTime()) }
            runIO { delay(getTwoTime()) }
            OverManager.log("向下滑动")
            AssistsWindowManager.switchNotTouchableAll()
            runIO { delay(856) }
            val x = ScreenUtils.getAppScreenWidth() / 2F
            val distance = ScreenUtils.getAppScreenHeight() / 2F
            val startY = distance + distance / 2F
            val endY = distance - distance / 2F
            OverManager.log("滑动：$x/$startY,$x/$endY")
            Assists.gesture(
                    floatArrayOf(x, startY), floatArrayOf(x, endY), 0, 1500L
            )
            AssistsWindowManager.switchTouchableAll()

            return@next Step.get(StepTag.STEP_6)

        }.next(StepTag.STEP_6) {
            runIO { delay(getOneTime()) }

            val x = java.util.Random().nextInt(ScreenUtils.getAppScreenWidth()).toFloat()
            val y = 500 + java.util.Random().nextInt(ScreenUtils.getAppScreenWidth() - 800).toFloat()
            val clickTime = 54L + java.util.Random().nextInt(60)
            Assists.gestureClick(x, y, clickTime)
            runIO { delay(getTwoTime()) }

            return@next Step.get(StepTag.STEP_7)
        }.next(StepTag.STEP_7) {
            getInterest({ interestModel ->
                mImageInterestModel = interestModel
                StepManager.execute(InstagramSocial::class.java, StepTag.STEP_8, begin = true)
            })
            {
                StepManager.execute(InstagramSocial::class.java, StepTag.STEP_5, begin = true)

            }
            return@next Step.none
        }.next(StepTag.STEP_8) {
            var time = 0
            if (mImageInterestModel.isToLike) {
                time = 3000 + Random.nextInt(5000)
            }
            if (mImageInterestModel.isToFollow) {
                time += Random.nextInt(5000)
            }
            if (mImageInterestModel.isToComment) {
                time += Random.nextInt(5000)
            }
            runIO { delay(time.toLong()) }

            if (mImageInterestModel.isToLike) {
                OverManager.log("点赞")
                runIO { delay(getOneTime()) }
                Assists.findById("com.instagram.android:id/like_button").forEach {
                    Assists.gestureViewClick(it)
                    runIO { delay(getTwoTime()) }
                    return@next Step.get(StepTag.STEP_9)
                }
            }
            return@next Step.get(StepTag.STEP_9)
        }.next(StepTag.STEP_9) {
            if (mImageInterestModel.isToFollow) {
                runIO { delay(getOneTime()) }
                Assists.findById("com.instagram.android:id/inline_follow_button").forEach {
                    Assists.gestureViewClick(it)
                    runIO { delay(getTwoTime()) }
                    return@next Step.get(StepTag.STEP_10)
                }
            }
            return@next Step.get(StepTag.STEP_10)
        }.next(StepTag.STEP_10) {
//            if (mImageInterestModel.isToComment) {
            return@next Step.get(StepTag.STEP_11)
//            }
//            return@next Step.get(StepTag.STEP_5)
        }.next(StepTag.STEP_11) {
            runIO { delay(getOneTime()) }
            OverManager.log("查找评论按钮")
            Assists.findByText("Comment").forEach {
                it.click()
                runIO { delay(getTwoTime()) }
                return@next Step.get(StepTag.STEP_12)
            }
            runIO { delay(getTwoTime()) }

            return@next Step.get(StepTag.STEP_5)
        }.next(StepTag.STEP_12) {
            runIO { delay(getOneTime()) }

            getComment({ commentModel ->
                mCommentModel = commentModel
                StepManager.execute(InstagramSocial::class.java, StepTag.STEP_13, begin = true)
            })
            {
                StepManager.execute(InstagramSocial::class.java, StepTag.STEP_15, begin = true)
            }
            return@next Step.none
        }.next(StepTag.STEP_13) {
            runIO { delay(getOneTime()) }
            OverManager.log("发送评论")
            if (mCommentModel.comment.isNullOrEmpty()) {
                return@next Step.get(StepTag.STEP_14)
            }
            Assists.findById("com.instagram.android:id/layout_comment_thread_edittext").forEach {
                it.log()
                Assists.gestureViewClick(it)

                runIO { delay(getTwoTime()) }

                for (s in mCommentModel.comment) {
                    it.paste(s.toString())
                    runIO { delay(getInputTime()) }
                }
                return@next Step.get(StepTag.STEP_14)
            }
            return@next Step.get(StepTag.STEP_14)
        }.next(StepTag.STEP_14) {
            OverManager.log("点击发送")
            runIO { delay(getOneTime()) }
            Assists.gestureKeyboardClick()
            return@next Step.get(StepTag.STEP_15)
        }.next(StepTag.STEP_15) {
            runIO { delay(getTwoTime()) }
            Assists.back()
            runIO { delay(getTwoTime()) }
            Assists.back()
            runIO { delay(getTwoTime()) }
            return@next Step.get(StepTag.STEP_5)
        }.next(StepTag.STEP_100) {
            OverManager.log("结束")
            return@next Step.none
        }
    }
}